package com.imy.touch.helper;

import android.os.SystemClock;
import android.util.Log;
import android.view.InputDevice;
import android.view.MotionEvent;

import com.imy.touch.bean.GesturePoint;

import java.util.ArrayList;
import java.util.List;


public class MotionEventHelper {
    private String TAG="MotionEventHelper";
    private static final int MAX_GESTURE = 10;
    private static MotionEventHelper gCore = new MotionEventHelper();
    private List<GesturePoint> pointList =new ArrayList<>();
    private long downTime;
    public static MotionEventHelper get() {
        return gCore;
    }

    public MotionEvent createEvent(GesturePoint point){
        if (pointList.size() >= MAX_GESTURE) {
            //Log.i(TAG, "当前超过10个手势,返回");
            return null;
        }
        addPoint(point);
        int index = pointList.size() - 1;
        return getMotionEvent(pointList.get(index),index);
    }
    private static MotionEvent getSingleMotionEvent(long downTime, long eventTime, int action,
                                                    float x, float y, int id) {

        MotionEvent.PointerProperties properties = new MotionEvent.PointerProperties();
        properties.id = id;
        properties.toolType = MotionEvent.TOOL_TYPE_FINGER;

        MotionEvent.PointerCoords coords = new MotionEvent.PointerCoords();
        coords.pressure = 1;
        coords.size = 1;
        coords.x = x;
        coords.y = y;

        return MotionEvent.obtain(downTime, eventTime, action, 1,
                new MotionEvent.PointerProperties[]{properties}, new MotionEvent.PointerCoords[]{coords},
                0, 0, 1.0f, 1.0f, 0, 0, InputDevice.SOURCE_TOUCHSCREEN, 0);
    }
    private MotionEvent getMotionEvent(GesturePoint trigger_point, int index) {
        if (pointList.size() == 1) {
            GesturePoint point = pointList.get(0);
            return getSingleMotionEvent(downTime,downTime,point.action,point.x,point.y,point.id);
        }
        return null;
    }

    private void addPoint(GesturePoint point) {
        //判断当前是第几个手指
        int size = pointList.size();
        if (size == 0) {
            //当前无手指 按下的是第一个手指 需记录按下时间downTime
            downTime = SystemClock.uptimeMillis();
        }
        //为手势创建id
        pointList.add(point);
    }

    public synchronized MotionEvent releasedMotionEvent(GesturePoint targetPoint) {
        //取出对应手势的缓存
        //需判断当前缓存了几个手指
        if (pointList.size() == 1) {
            GesturePoint point = pointList.get(0);
            long point_down_time = point.point_down_time;
            MotionEvent singleMotionEvent = getSingleMotionEvent(point_down_time,
                    SystemClock.uptimeMillis(), point.action, point.x, point.y, point.id);
            Log.i(TAG, "即将释放-->只有一个手指在屏幕上 :" + singleMotionEvent.toString());
            pointList.remove(point);
            return singleMotionEvent;
        } else {
//            Object[] objects = setTargetPointReleased(event.getEvent_type(), event.getX(), event.getY());//即将释放的键位//注入事件
//            if (objects != null) {
//
//                Point point = (Point) objects[0];
//                int index = (int) objects[1];
//                Log.i(TAG, "即将释放-->查找到将要释放的键位" + JsonParser.objectToString(point) + "  index" + objects[1]);
//                MotionEvent multiMotionEvent = createMultiMotionEvent(point, index);
//                //在移除缓存手势;
//                pointList.remove(point);
//                return multiMotionEvent;
//            }
            return null;
        }
    }

}
